package com.ruoyi.system.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import org.apache.poi.ss.usermodel.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.QCktongji;
import com.ruoyi.system.service.IQCktongjiService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 窗口人员统计Controller
 * 
 * @author ruoyi
 * @date 2024-09-13
 */
@Controller
@RequestMapping("/system/cktongji")
public class QCktongjiController extends BaseController
{
    private String prefix = "system/cktongji";

    static List<QCktongji> listExcel =  new ArrayList<QCktongji>();

    @Autowired
    private IQCktongjiService qCktongjiService;

    @RequiresPermissions("system:cktongji:view")
    @GetMapping()
    public String cktongji()
    {
        return prefix + "/cktongji";
    }



    public String getAbsoluteFile(String filename) {
        String downloadPath = RuoYiConfig.getDownloadPath() + filename;
        File desc = new File(downloadPath);
        if (!desc.getParentFile().exists()) {
            desc.getParentFile().mkdirs();
        }
        return downloadPath;
    }

    public void receiveReward(QCktongji qCktongji)
    {
        if ( qCktongji.getNomanyihj() == null || qCktongji.getManyi() == null ){
            qCktongji.setMylv("0%");
            return;

        }
        if ( qCktongji.getNomanyihj().equals("0")  ) {
              qCktongji.setMylv("100%");
              return;
        }
        if ( ( qCktongji.getNomanyihj().equals("0")  && qCktongji.getNomanyihj().equals("0"))  ) {
            qCktongji.setMylv("100%");
            return ;
        }
        long sum =(qCktongji.getNomanyihj())+ (qCktongji.getManyi()) ;

           long lv =  (qCktongji.getManyi()/sum)*100 ;

        qCktongji.setMylv(lv+"%");

        return;




    }


    /**
     * 查询窗口人员统计列表
     */
    @RequiresPermissions("system:cktongji:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(QCktongji qCktongji)
    {
       // startPage();
        List<QCktongji> list = qCktongjiService.selectQCktongjiList(qCktongji);
        list.forEach(item -> receiveReward(item));
        this.listExcel = list ;
        return getDataTable(list);
    }

    /**
     * 导出窗口人员统计列表
     */
    @RequiresPermissions("system:cktongji:export")
    @Log(title = "窗口人员统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(QCktongji qCktongji)
    {
        List<QCktongji> data = this.listExcel ;


        String filename = new String("窗口人员统计(template)" + ".xlsx");


        Path sourcePath = Paths.get(getAbsoluteFile(filename));
        Path destinationPath = Paths.get(getAbsoluteFile("窗口人员统计.xlsx"));

        try {
            Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
            System.out.println("File copied successfully.");
        } catch (IOException e) {
            System.out.println("Error occurred while copying file.");
            e.printStackTrace();
        }

        try {
            FileInputStream file = new FileInputStream(getAbsoluteFile("窗口人员统计.xlsx"));
            Workbook workbook = WorkbookFactory.create(file);
            Sheet sheet = workbook.getSheetAt(0);
            int rowIndex = 5;  //从第6行开始写
            for (int i = 0; i < data.size(); i++) {
                QCktongji tmp = new QCktongji();
                tmp = (QCktongji) data.get(i);


                Row row = sheet.getRow(i + rowIndex);
                int colIndex = 0;
                Cell cell = row.getCell(0);
                cell.setCellValue(tmp.getRyname()); //窗口人员

                cell = row.getCell(1);
                cell.setCellValue("100%");   //分数

                cell = row.getCell(2);
                cell.setCellValue(tmp.getManyisum());   //满意

                cell = row.getCell(3);
                cell.setCellValue(tmp.getTaidusum());   //一般

                cell = row.getCell(4);
                cell.setCellValue(tmp.getWeijiejue());   //不满意

                cell = row.getCell(5);
                cell.setCellValue(tmp.getNomanyihj());   //不满意合计

                cell = row.getCell(6);
                cell.setCellValue(tmp.getNomanyihj());   //不满意合计

                cell = row.getCell(7);
                cell.setCellValue(tmp.getMylv());   //满意率


            }
            Row row = sheet.getRow(3);
            Cell cell = row.getCell(0);
            // 修改单元格的内容
            cell.setCellValue("统计开始时间：" + qCktongji.getStartTime());

            row = sheet.getRow(3);
            cell = row.getCell(3);
            // 修改单元格的内容
            cell.setCellValue("统计结束时间：" + qCktongji.getEndTime());

            file.close();

            FileOutputStream outFile = new FileOutputStream(getAbsoluteFile("窗口人员统计.xlsx"));
            workbook.write(outFile);
            outFile.close();

            System.out.println("Excel file modified successfully.");

        } catch (Exception e) {
            e.printStackTrace();
        }

        return AjaxResult.success("窗口人员统计.xlsx");
    }

    /**
     * 新增窗口人员统计
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存窗口人员统计
     */
    @RequiresPermissions("system:cktongji:add")
    @Log(title = "窗口人员统计", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(QCktongji qCktongji)
    {
        return toAjax(qCktongjiService.insertQCktongji(qCktongji));
    }

    /**
     * 修改窗口人员统计
     */
    @RequiresPermissions("system:cktongji:edit")
    @GetMapping("/edit/{cktjid}")
    public String edit(@PathVariable("cktjid") Long cktjid, ModelMap mmap)
    {
        QCktongji qCktongji = qCktongjiService.selectQCktongjiByCktjid(cktjid);
        mmap.put("qCktongji", qCktongji);
        return prefix + "/edit";
    }

    /**
     * 修改保存窗口人员统计
     */
    @RequiresPermissions("system:cktongji:edit")
    @Log(title = "窗口人员统计", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(QCktongji qCktongji)
    {
        return toAjax(qCktongjiService.updateQCktongji(qCktongji));
    }

    /**
     * 删除窗口人员统计
     */
    @RequiresPermissions("system:cktongji:remove")
    @Log(title = "窗口人员统计", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(qCktongjiService.deleteQCktongjiByCktjids(ids));
    }
}
